package com.zhuxiang.test1;


/**
 * 编程实现控制台版并支持两人对战的五子棋游戏。
 */
public class Gobang {

    private  String black = " ● ";
    private  String white = " ○ ";
    //棋盘大小
    private int len = 17;
    //确认是白方还是黑方
    private String color;
    //利用二维数组绘制棋盘
    private Object[][] arr = new Object[len][len];

    //初始化对象时就绘制棋盘
    public Gobang() {
        init();
    }

    /**
     * 绘制棋盘方法
     */
    public void init(){
        System.out.println("五子棋棋盘绘制如下：");
        int temp = 97;
        int temp1 = 97;
        for(int i = 0; i<arr.length; i++){
            for(int j = 0; j<arr[i].length;j++){
                arr[i][j] = " + ";
            }
            arr[0][0] = "   ";
        }
        for(int m = 1; m<arr[0].length; m++){
            if(m<=10){
                arr[0][m] = " "+(m-1)+" ";
            }else{
                arr[0][m] = " "+(char)temp+" ";
                temp++;
            }
        }
        for(int n = 1; n<arr.length; n++){
            if(n<=10){
                arr[n][0] = " "+(n-1)+" ";
            }else{
                arr[n][0] = " "+(char)temp1+" ";
                temp1++;
            }
        }
        print();
    }

    /**
     * 打印棋盘方法
     */
    public void print(){
        for(int i = 0; i<arr.length; i++){
            for(int j = 0; j<arr[i].length;j++){
                System.out.print(arr[i][j]);
            }
            System.out.println();
        }
    }

    /**
     * 解析坐标方法
     * @param point
     * @return
     */
    public Point getPoint(String point){
        String[] points = point.split(" ");
        if (points.length == 2 ){
            int x = Integer.parseInt(points[0])+1;
            int y = Integer.parseInt(points[1])+1;
            return new Point(x,y);
        }
        return new Point(0,0);

    }

    /**
     * 下棋方法
     * @param point
     * @param color
     * @return
     */
    public boolean playChess(Point point,boolean color){
        //判断是否越界
        if (point.getX() < 0 || point.getY() >16 || point.getX() >16 || point.getY() < 0){
            System.out.println("坐标越界了");
            return false;
        }
        //判断落子的地方是否有其他的棋子
        if (arr[point.getX()][point.getY()] == arr[0][0]){
            System.out.println("亲，坐标输入有误，请按照规则输入！");
            return false;
        }
        if (arr[point.getX()][point.getY()] != " + "){
            System.out.println("此次已有棋子了");
            return false;
        }
        arr[point.getX()][point.getY()] = color ? white : black;
        return true;
    }

    /**
     * 判断是否取胜
     * @param point
     * @param color
     * @return
     */
    public boolean isWin(Point point,boolean color){

        int porUp = 0;     //↑
        int porDown = 0;   //↓
        int tranLeft = 0;  //←
        int tranRight = 0; //→
        int forUp = 0;     //↗
        int forDown = 0;   //↙
        int backUp = 0;    //↖
        int backDown = 0;  //↘
        //纵向上 ↑
        for (int i = 0; i < 5; i++){
            if (point.getX() - i < 0){
                break;
            }
            if (arr[point.getX()-i][point.getY()].equals(color ? white : black)){
                porUp++;
            }else {
                break;
            }
        }
        //纵向下 ↓
        for (int i = 0; i < 5; i++){
            if (point.getX() + i > 16){
                break;
            }
            if (arr[point.getX()+i][point.getY()].equals(color ? white : black)){
                porDown++;
            }else {
                break;
            }
        }
        //横向左 ←
        for (int i = 0; i < 5; i++){
            if (point.getY() - i < 0){
                break;
            }
            if (arr[point.getX()][point.getY()-i].equals(color ? white : black)){
                tranLeft++;
            }else {
                break;
            }
        }
        //横向右 →
        for (int i = 0; i < 5; i++){
            if (point.getY() + i > 16 ){
                break;
            }
            if (arr[point.getX()][point.getY()+i].equals(color ? white : black)){
                tranRight++;
            }else {
                break;
            }
        }
        //正斜上 ↗
        for (int i = 0; i < 5; i++){
            if (point.getX() - i < 0 || point.getY() + i >16){
                break;
            }
            if (arr[point.getX()-i][point.getY()+i].equals(color ? white : black)){
                forUp++;
            }else {
                break;
            }
        }
        //正斜下 ↙
        for (int i = 0; i < 5; i++){
            if (point.getX() + i > 16 || point.getY() - i < 0){
                break;
            }
            if (arr[point.getX()+i][point.getY()-i].equals(color ? white : black)){
                forDown++;
            }else {
                break;
            }
        }
        //反斜上 ↖
        for (int i = 0; i < 5; i++){
            if (point.getX() - i < 0 || point.getY() - i < 0){
                break;
            }
            if (arr[point.getX()-i][point.getY()-i].equals(color ? white : black)){
                backUp++;
            }else {
                break;
            }
        }
        //反斜下 ↘
        for (int i = 0; i < 5; i++){
            if (point.getX() + i > 16 || point.getY() + i > 16){
                break;
            }
            if (arr[point.getX()+i][point.getY()+i].equals(color ? white : black)){
                backDown++;
            }else {
                break;
            }
        }
        if (porUp + porDown > 5 || tranLeft + tranRight >5 || forDown + forUp >5 || backDown + backUp >5){
            return true;
        }
        return false;
    }

}
